package com.learn.project2.config.aspectj;


import com.learn.project2.bean.MarketAdmin;
import com.learn.project2.bean.MarketLog;
import com.learn.project2.mapper.MarketLogMapper;
import com.learn.project2.model.bo.admin.admin.AdminCreateBO;
import com.learn.project2.model.bo.admin.admin.AdminUpdateBO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @description:
 * @author zhizhuke
 * @date 2022-01-09 16-26
 * @version 1.0
 */
@Aspect
@Component
public class AdminControllerAspect {

    @Autowired
    MarketLogMapper logMapper;


    @Pointcut("execution(* com.learn.project2.controller.admin.AdminController.update(..))")
    public void adminUpdate() {//只需要这个方法的方法名
    }
    @Pointcut("execution(* com.learn.project2.controller.admin.AdminController.create(..))")
    public void adminCreate() {//只需要这个方法的方法名
    }
    @Pointcut("execution(* com.learn.project2.controller.admin.AdminController.delete(..))")
    public void adminDelete() {//只需要这个方法的方法名
    }

    @Transactional
    @Around("adminUpdate()")
    public Object update(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[1]);
        AdminUpdateBO bo = ((AdminUpdateBO) args[0]);

        String ip = request.getRemoteAddr();
        Date date = new Date();

        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();

        MarketLog marketLog = new MarketLog(null,admin.getUsername(),ip,1,"编辑管理员",false,bo.getUsername(),null, date, date,false);


        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();

        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }
    @Transactional
    @Around("adminCreate()")
    public Object create(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[1]);
        AdminCreateBO bo = ((AdminCreateBO) args[0]);

        String ip = request.getRemoteAddr();
        Date date = new Date();
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();
        MarketLog marketLog = new MarketLog(null,admin.getUsername(),ip,1,"添加管理员",false,bo.getUsername(),null, date, date,false);


        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();

        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }
    @Transactional
    @Around("adminDelete()")
    public Object delete(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[1]);
        AdminUpdateBO bo = ((AdminUpdateBO) args[0]);

        String ip = request.getRemoteAddr();
        Date date = new Date();
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();
        MarketLog marketLog = new MarketLog(null,admin.getUsername(),ip,1,"删除管理员",false,bo.getUsername(),null, date, date,false);


        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();

        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }




}
